package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.PurchaserecordExample;
import com.campusify.pojo.PurchaserecordExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.PurchaserecordMapper;
import com.campusify.pojo.Purchaserecord;
import com.campusify.service.PurchaseRecordService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class PurchaseRecordServiceImpl implements PurchaseRecordService {

	@Autowired
	private PurchaserecordMapper purchaseRecordMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Purchaserecord> findAll() {
		return purchaseRecordMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Purchaserecord> page=   (Page<Purchaserecord>) purchaseRecordMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Purchaserecord purchaseRecord) {
		purchaseRecordMapper.insert(purchaseRecord);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Purchaserecord purchaseRecord){
		purchaseRecordMapper.updateByPrimaryKey(purchaseRecord);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Purchaserecord findOne(Integer recordID){
		return purchaseRecordMapper.selectByPrimaryKey(recordID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] recordIDs) {
		for(Integer recordID:recordIDs){
			purchaseRecordMapper.deleteByPrimaryKey(recordID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Purchaserecord purchaseRecord, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		PurchaserecordExample example=new PurchaserecordExample();
		Criteria criteria = example.createCriteria();
		
		if(purchaseRecord!=null){			
						if(purchaseRecord.getBuytime()!=null && purchaseRecord.getBuytime().length()>0){
				criteria.andBuytimeLike("%"+purchaseRecord.getBuytime()+"%");
			}	
		}
		
		Page<Purchaserecord> page= (Page<Purchaserecord>)purchaseRecordMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
